import java.awt.*;

public class Pacman {
    private int row, col;
    private int dirRow = 0, dirCol = 0;
    private int nextDirRow = 0, nextDirCol = 0;

    public Pacman(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public void setDirection(int keyCode) {
        switch (keyCode) {
            case 37: // 左
                nextDirRow = 0; nextDirCol = -1; break;
            case 38: // 上
                nextDirRow = -1; nextDirCol = 0; break;
            case 39: // 右
                nextDirRow = 0; nextDirCol = 1; break;
            case 40: // 下
                nextDirRow = 1; nextDirCol = 0; break;
        }
    }

    public boolean move(Map map) {
        // 只有在可以转向时才改变方向
        int newRow = row + nextDirRow;
        int newCol = col + nextDirCol;
        if (!map.isWall(newRow, newCol)) {
            dirRow = nextDirRow;
            dirCol = nextDirCol;
        }
        // 再按当前方向移动一步
        newRow = row + dirRow;
        newCol = col + dirCol;
        if (!map.isWall(newRow, newCol)) {
            row = newRow;
            col = newCol;
            return map.eatDot(row, col); // 返回是否吃到豆子
        }
        return false;
    }

    public void draw(Graphics g) {
        int size = Map.TILE_SIZE;
        g.setColor(Color.YELLOW);
        g.fillArc(col * size, row * size, size, size, 30, 300);
    }

    public int getRow() { return row; }
    public int getCol() { return col; }
    public void setPosition(int row, int col) {
        this.row = row;
        this.col = col;
    }

    public int[] getFront(int n, Map map) {
        int r = row, c = col;
        for (int i = 0; i < n; i++) {
            int nr = r + dirRow;
            int nc = c + dirCol;
            if (map.isWall(nr, nc)) break;
            r = nr;
            c = nc;
        }
        return new int[]{r, c};
    }
} 